package com.yidu.t286.dao;

import com.yidu.t286.domain.Fun;
import org.apache.ibatis.annotations.*;

import java.util.List;

/*
 * @description:
 * @author:文杰
 * @date  :2021/7/30 0030上午 8:59
 */
@Mapper
public interface FunDao {

    /**
     * 查询父功能
     * @param roleId 角色编号
     * @return 功能集合
     */
    @Select("SELECT f.funId as  funId,f.funType funType,f.funName funName,f.icon icon,rr.rightId AS rightId,rr.roleId as roleId ,f.href href,f.paths paths,f.components components " +
            " FROM (SELECT * FROM fun WHERE funType='Folder') f " +
            " JOIN  ( SELECT * FROM role_right WHERE roleId=#{roleId}) rr on f.funId=rr.funId ")
    @Results(id = "roleTreeMap", value={
            @Result(id=true,property = "Id",column = "funId"),
            @Result(property = "title",column = "funName"),
            @Result(property = "href",column = "href"),
            @Result(property = "checked",column = "rightId"),
            @Result(property = "icon",column = "icon"),
            @Result(property = "funType",column = "funType"),
            @Result(property = "paths",column = "paths"),
            @Result(property = "components",column = "components"),
            @Result(property = "parentId",column = "parentId"),
            @Result(property = "child",column = "roleId=roleId,funId=funId",
                    one = @One(select = "com.yidu.t286.dao.FunDao.selectChildren"))
    })
    public List<Fun> selectFolder(@Param("roleId") int roleId);

    /**
     * 查询父功能(给权限树使用)
     * @param roleId 角色编号
     * @return 功能集合
     */
    @Select("SELECT f.funId as  funId,f.funType funType,f.funName funName,f.icon icon,rr.rightId AS rightId,rr.roleId as roleId,f.href href,f.paths paths,f.components components " +
            " FROM (SELECT * FROM fun WHERE funType='Folder') f " +
            " LEFT JOIN  ( SELECT * FROM role_right WHERE roleId=#{roleId}) rr on f.funId=rr.funId ")
    @ResultMap("roleTreeMap")
    public List<Fun> selectFolders(@Param("roleId") int roleId);

    /**
     * 查询子功能
     * @param funId 功能编号
     * @return 功能集合
     */
    @Select("SELECT f.funId as funId,f.parentId parentId,f.funName as funName,rr.rightId AS rightId,rr.roleId as roleId,href,f.icon icon,f.paths paths,f.components components " +
            " FROM (SELECT * FROM fun WHERE funType='Document' AND parentId=#{funId}) f" +
            " LEFT JOIN  ( SELECT * FROM role_right where roleId=#{roleId,jdbcType=INTEGER}) rr on f.funId=rr.funId ")
    @ResultMap(value = "roleTreeMap")
    public List<Fun> selectChildren(@Param("funId") String funId, @Param("roleId") int roleId);

    /**
     * 删除对应角色所有的权限
     * @param roleId 角色Id
     * @return 受影响的行数
     */
    @Delete("delete from role_right where roleId=#{roleId}")
    public int deleteAllFun(@Param("roleId") int roleId);

    /**
     * 添加权限
     * @param roleId 角色编号
     * @param Id  功能编号
     * @return 受影响的行数
     */
    @Insert("insert into role_right(rightId,roleId,funId) values(seq_right.nextval,#{roleId},#{funId})")
    public int addFunByRoleId(@Param("roleId") int roleId, @Param("funId") String Id);


}
